<template>
  <div class="app-container">
    <div class="search-container">
      <el-form :model="formData" :inline="true">
        <el-form-item label="BOM编号" prop="bomCode">
          <el-input
            v-model="formData.bomCode"
            placeholder="请输入BOM编号"
            clearable
            :disabled="true"
          />
        </el-form-item>
        <el-form-item label="BOM名称" prop="bomName">
          <el-input
            v-model="formData.bomName"
            placeholder="请输入BOM名称"
            clearable
            :disabled="true"
          />
        </el-form-item>
        <el-form-item label="成品物料" prop="bomName">
          <el-input
            v-model="formData.bomName"
            placeholder="请输入BOM名称"
            clearable
            :disabled="true"
          />
        </el-form-item>
      </el-form>
    </div>

    <el-card shadow="never" class="table-container">
      <template #header>
        <el-button type="success" plain @click="openDialog('')"
          ><i-ep-plus />新增</el-button
        >
      </template>

      <el-table
        ref="dataTableRef"
        v-loading="loading"
        :data="tableData"
        highlight-current-row
        border
        @selection-change="handleSelectionChange"
        row-key="id"
        :indent="15"
        default-expand-all
        :tree-props="{
          children: 'nodes',
          hasChildren: 'hasChildren',
        }"
      >
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column
          label="组件物料编号"
          prop="materialCode"
          min-width="100"
          show-overflow-tooltip
        >
        </el-table-column>
        <el-table-column
          label="组件物料名称"
          prop="materialName"
          min-width="100"
          show-overflow-tooltip
        >
          <template #default="scope">
            {{ scope.row.materialCode }} - {{ scope.row.materialName }}
          </template>
        </el-table-column>
        <el-table-column
          label="虚拟件"
          prop="virtualParts"
          min-width="100"
          show-overflow-tooltip
          align="center"
        >
          <template #default="scope">
            <el-tag type="success" v-if="scope.row.virtualParts === 1"
              >是</el-tag
            >
            <el-tag type="info" v-else>否</el-tag>
          </template>
        </el-table-column>
        <el-table-column
          label="数量"
          prop="matQuantity"
          min-width="100"
          show-overflow-tooltip
        >
        </el-table-column>
        <el-table-column
          label="数量单位"
          prop="quantityUnit"
          min-width="100"
          show-overflow-tooltip
          align="center"
        >
          <template #default="scope">
            {{ useDictionary("MeasurementUnit", scope.row.quantityUnit) }}
          </template>
        </el-table-column>
        <el-table-column
          label="备注"
          prop="remarks"
          min-width="100"
          show-overflow-tooltip
        >
        </el-table-column>
        <el-table-column fixed="right" label="操作" width="240">
          <template #default="scope">
            <el-button
              type="primary"
              size="small"
              link
              @click="openDialog(scope.row)"
              ><i-ep-edit />编辑</el-button
            >
            <el-button
              type="primary"
              size="small"
              link
              @click="addSubordinates(scope.row)"
              ><i-ep-plus />添加下级</el-button
            >
            <el-button
              type="primary"
              size="small"
              link
              @click="handleTableDelete(scope.row.id)"
              ><i-ep-delete />删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>

      <!-- <el-pagination
        :background="true"
        :current-page="pageable.pageNum"
        :page-size="pageable.pageSize"
        :page-sizes="[10, 25, 50, 100]"
        :total="pageable.total"
        layout="total, sizes, prev, pager, next, jumper"
        class="mt-[20px]"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      /> -->
    </el-card>
    <specification-edit
      :visible="addDialog.visible"
      :title="addDialog.title"
      :data="addDialog.data"
      :addData="addDialog.addData"
      :plantId="addDialog.plantId"
      :parentId="addDialog.parentId"
      @reset-table="(id:string) => (id?.length ? getTableList() : reset())"
      :is-readonly="addDialog.readvisible"
      @change-visible="changeVisible"
    />
  </div>
</template>

<!-- 手动挂载左侧菜单高亮 -->
<script lang="ts">
import { defineComponent } from "vue";
export default defineComponent({
  beforeRouteEnter(to, from, next) {
    to.meta.defaultActive = "/mes/mesbom/content";
    if (!to.query.id) {
      to.meta.title = "BOM明细";
    } else {
      to.meta.title = "BOM明细";
    }

    next();
  },
});
</script>
<script setup lang="ts">
import { useTable, useDownload, useDictionary } from "@/utils/useTable";
import request from "@/utils/request";
import { useRoute } from "vue-router";

const route = useRoute();
defineOptions({
  name: "MESMESBOMSPECIFICATION",
  inheritAttrs: false,
});

// 请求参数
const requestApi = "/mes/mesbomdetail/getTree";

// 是否需要分页
const pagination = true;

const initParam = {};

// 初始化搜索参数数据
const searchParamInit = {
  bomId: route.query.id,
};

const deleteParam = {
  api: "/mes/mesbomdetail/delete",
  data: {},
};

// 数据请求错误后处理
function requestError() {}

const dataCallBack = ref(undefined); // 无需二次数据处理&不传赞数也行

// 表格操作 Hooks
const {
  tableData,
  pageable,
  searchParam,
  ids,
  getTableList,
  search,
  reset,
  handleSizeChange,
  handleCurrentChange,
  handleSelectionChange,
  handleTableDelete,
  loading,
}: any = useTable(
  requestApi,
  initParam,
  searchParamInit,
  pagination,
  dataCallBack.value,
  requestError,
  deleteParam
);
const formData: any = ref({});
onMounted(() => {
  searchParam.value.bomId = route.query.id;

  getTableList();
  // route  /mesbom/get/{id}
  if (route.query.id) {
    getBomInfo(route.query.id);
  }
});

async function getBomInfo(id: string) {
  request({
    url: `/mes/mesbom/get/${id}`,
    method: "get",
  }).then((res) => {
    formData.value = res.data;
  });
}
const addDialog: any = reactive({
  visible: false,
  readvisible: false,
  title: "",
  data: {},
  plantId: "",
  addData: {},
  parentId: "",
});

// 查询详情
async function getInfo(id: string) {
  request({
    url: `/mes/mesbomdetail/get/${id}`,
    method: "get",
  })
    .then((data) => {
      addDialog.data = data.data;
      addDialog.visible = true;
    })
    .catch((res) => {
      console.log(res);
    });
}

/** 打开BOM明细表单弹窗 */
function openDialog(roleObj: any) {
  if (roleObj) {
    addDialog.title = "修改BOM明细";
    addDialog.plantId = formData.value.plantId;
    getInfo(roleObj.id);
  } else {
    addDialog.title = "新增BOM明细";
    addDialog.visible = true;
    addDialog.plantId = formData.value.plantId;
    addDialog.addData = formData.value;
    addDialog.readvisible = false;
  }
}

/** 打开BOM明细表单弹窗 详情 */
function addSubordinates(roleObj?: any) {
  addDialog.title = "新增BOM明细下级";
  addDialog.visible = true;
  addDialog.plantId = formData.value.plantId;
  addDialog.addData = formData.value;
  addDialog.parentId = roleObj.id;

  addDialog.readvisible = false;
}

function changeVisible(value: boolean) {
  addDialog.visible = value;
  if (!value) {
    addDialog.data = {};
    addDialog.addData = {};
  }
}
</script>
